package com.uic.client;

import com.uic.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class UIC implements EntryPoint {

  /**
   * Create a remote service proxy to talk to the server-side Greeting service.
   */
  private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
  
  
  private VerticalPanel mainPanel = new VerticalPanel();
  private HorizontalPanel commandPanel = new HorizontalPanel();
  
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    final Button sendButton = new Button("Send");
    final TextBox commandField = new TextBox();
    commandField.setText("help");
    final FlexTable responseFlexTable = new FlexTable();

    // We can add style names to widgets
    sendButton.addStyleName("sendButton");
    
    commandPanel.add(commandField);
    commandPanel.add(sendButton);
    mainPanel.add(commandPanel);

    mainPanel.add(responseFlexTable);
    RootPanel.get("loadingMsg").setVisible(false);
    RootPanel.get("cliResponseDiv").add(mainPanel);
    
    responseFlexTable.setText(0, 0, "Command");
    responseFlexTable.setText(0, 1, "Response");
    responseFlexTable.getRowFormatter().addStyleName(0, "tableHeader");
    responseFlexTable.addStyleName("table");

    // Focus the cursor on the name field when the app loads
    commandField.setFocus(true);
    commandField.selectAll();
    
    // Create a handler for the sendButton and commandField
    class MyHandler implements ClickHandler, KeyUpHandler {
      /**
       * Fired when the user clicks on the sendButton.
       */
      public void onClick(ClickEvent event) {
        sendNameToServer();
      }

      /**
       * Fired when the user types in the commandField.
       */
      public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
          sendNameToServer();
        }
      }

      /**
       * Send the name from the commandField to the server and wait for a response.
       */
      private void sendNameToServer() {

    	final int row = responseFlexTable.getRowCount();
        
        String textToServer = commandField.getText();
        responseFlexTable.setText(row, 0, textToServer);

        
        if (!FieldVerifier.isValidName(textToServer)) {
          responseFlexTable.getCellFormatter().addStyleName(row, 1, "errorCell");
          responseFlexTable.setText(row, 1, "Please enter at least four characters");
        } else {
	
	        // Then, we send the input to the server.
	        greetingService.greetServer(textToServer, new AsyncCallback<String>() {
	          public void onFailure(Throwable caught) {
	            // Show the RPC error message to the user
	        	responseFlexTable.getCellFormatter().addStyleName(row, 1, "errorCell");
	        	responseFlexTable.setText(row, 1, "Remote Procedure Call - Failure");
	          }
	
	          public void onSuccess(String result) {
	        	responseFlexTable.setHTML(row, 1, result);

	          }
	        });
        }
      }
    }

    // Add a handler to send the name to the server
    MyHandler handler = new MyHandler();
    sendButton.addClickHandler(handler);
    commandField.addKeyUpHandler(handler);
  }
}
